package me.sauce.folkpocket48.data.remote.utils

import com.orhanobut.logger.Logger
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
 * @author sauceWu .
 * @since 2017/12/22 14:58
 */

class LoggerInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        request.newBuilder()
                .addHeader("version", "4.1.0")
                .addHeader("User-Agent", "Mobile_Pocket")
                .addHeader("imei", "123223445")
                .build()

        when (request.method()) {
            "POST", "PUT" -> {
                if (request.body() != null && request.body().toString().isNotBlank()) {
                    val params = request.body().toString()
                    Logger.json(params)
                }
            }
            "GET" -> {
                val params = request.url().encodedPath()
                Logger.xml(params)
            }

        }
        val response = chain.proceed(chain.request())
        val res = response.body().toString()
        Logger.json(res)
        return response
    }
}
